package com.czl.module_storehouse.activity.inventory.details;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;

import com.czl.module_base.activity.BaseActivity;
import com.czl.module_storehouse.databinding.ActivityInventoryDetailsBinding;
import com.czl.module_storehouse.databinding.HeaderInventoryDetailsBinding;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.InventoryDetailsAdapter;
import com.czl.module_storehouse.bean.InventoryBean;
import com.czl.module_storehouse.event.InventoryEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author xubin
 */
public class InventoryDetailsActivity extends BaseActivity<ActivityInventoryDetailsBinding> {

    private InventoryDetailsAdapter mAdapter;
    private HeaderInventoryDetailsBinding mHeaderBinding;

    @Override
    protected ActivityInventoryDetailsBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityInventoryDetailsBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("盘点结果");
        mAdapter = new InventoryDetailsAdapter(R.layout.item_inventory_details, new ArrayList<>());
        mAdapter.setOnItemClickListener((adapter, view, position) -> {

        });
        binding.recyclerView.setAdapter(mAdapter);
        mHeaderBinding = HeaderInventoryDetailsBinding.inflate(getLayoutInflater());
        mAdapter.addHeaderView(mHeaderBinding.getRoot());
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(InventoryEvent event) {
        InventoryBean inventoryBean = event.getInventoryBean();
        initHeader(inventoryBean);
        mAdapter.addData(inventoryBean.getSortList());
    }

    @SuppressLint("SetTextI18n")
    private void initHeader(InventoryBean inventoryBean) {
        mHeaderBinding.clHeaderInfo.ivHeader.setImageResource(R.drawable.ic_icon_pandain_big);
        mHeaderBinding.clHeaderInfo.tvHeaderTitle.setText(inventoryBean.getInventoryName());
        mHeaderBinding.clHeaderInfo.tvHeaderCode.setText("盘点单：" + inventoryBean.getInventoryCode());
        mHeaderBinding.clHeaderInfo.tvHeaderDate.setText(inventoryBean.getGmtCreate());
        mHeaderBinding.tvHeaderCompany.setText("产权公司：" + inventoryBean.getPropCompanyName());
        mHeaderBinding.tvInvDate.setText("盘点时间：" + inventoryBean.getGmtCreate());
        mHeaderBinding.tvHeaderOutsize.setText("盘点范围：" + inventoryBean.getInventoryRangeStr());
        mHeaderBinding.tvInventoryNum.setText("盘点数：" + inventoryBean.getStockNum());
        mHeaderBinding.clHeaderCount.tvSurplusNum.setText(String.valueOf(inventoryBean.getSurplusNum()));
        mHeaderBinding.clHeaderCount.tvLossesNum.setText(String.valueOf(inventoryBean.getLossesNum()));
        mHeaderBinding.clHeaderCount.tvLossesValue.setText(inventoryBean.getLossesValue() + "元");
        mHeaderBinding.clHeaderCount.tvSurplusValue.setText(inventoryBean.getSurplusValue() + "元");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
    }
}
